package com.zy.service.bs.impl;

import com.zy.dao.bs.ActivityDao;
import com.zy.entity.bs.Activity;
import com.zy.pojo.exceptions.ServiceException;
import com.zy.utils.SerialCreateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zy.dao.bs.ActivityEnrollDao;
import com.zy.entity.enums.StatusEnum;
import com.zy.entity.bs.ActivityEnroll;
import com.zy.pojo.query.bs.ActivityEnrollQuery;
import com.zy.pojo.vos.common.PageVo;
import com.zy.service.base.impl.BaseServiceImpl;
import com.zy.service.bs.ActivityEnrollService;
import com.zy.utils.DateUtil;
import tk.mybatis.mapper.entity.Example;

@Service("activityEnrollService")
public class ActivityEnrollServiceImpl extends BaseServiceImpl<ActivityEnroll> implements ActivityEnrollService {

	@Autowired
	private ActivityEnrollDao activityEnrollDao;
	@Autowired
	private ActivityDao activityDao;

	@Override
	public PageVo pageActivityEnroll(ActivityEnrollQuery activityEnrollQuery, Integer start, Integer limit) {
		return new PageVo(start, limit, this.activityEnrollDao.countActivityEnroll(activityEnrollQuery), this.activityEnrollDao.listActivityEnroll(activityEnrollQuery, start, limit));
	}

	@Override
	public void saveActivityEnroll(ActivityEnroll activityEnroll) {
		Long activityId = activityEnroll.getActivityId();
		Activity activity = activityDao.getByPrimaryKey(activityEnroll.getActivityId());
		Integer number = activity.getNumber();
		if(number<getEnrollCount(activityId)+1){
			throw new ServiceException("超出过报名人数");
		}
		Long startTime =activity.getBeginTime();
		Long endTime = activity.getEndTime();
		Long now = DateUtil.getTime();
		if(!DateUtil.compare(now,startTime)||DateUtil.compare(now,endTime)){
			throw new ServiceException("不在活动范围内");
		}
		activityEnroll.setEnrollTime(now);
		activityEnroll.setEnrollStatus(0);
		activityEnroll.setCreateTime(now);
		activityEnroll.setModifyTime(now);
//		activityEnroll.setEnrollNo(SerialCreateUtil.generateByTime()+SerialCreateUtil.eachLength(4,getEndCode(DateUtil.getDateString())));
		activityEnrollDao.save(activityEnroll);

	}
	
	@Override
	public void updateActivityEnroll(ActivityEnroll activityEnroll) {
		activityEnroll.setModifyTime(DateUtil.getTime());
		this.activityEnrollDao.updateByPrimaryKeySelective(activityEnroll);
	}

	@Override
	public void removeActivityEnroll(long id) {
		try {
			this.activityEnrollDao.removeByPrimaryKey(id);
		} catch (Exception e) {
			log.info(e.getMessage(),e);
			ActivityEnroll activityEnroll = new ActivityEnroll();
			activityEnroll.setId(id);
			activityEnroll.setModifyTime(DateUtil.getTime());
			activityEnroll.setStatus(StatusEnum.DELETE.getValue());
			this.activityEnrollDao.updateByPrimaryKeySelective(activityEnroll);
		}
	}

	@Override
	public void recover(long id) {
		ActivityEnroll activityEnroll = new ActivityEnroll();
		activityEnroll.setId(id);
		activityEnroll.setModifyTime(DateUtil.getTime());
		activityEnroll.setStatus(StatusEnum.NORMAL.getValue());
		this.activityEnrollDao.updateByPrimaryKeySelective(activityEnroll);
	}
	@Override
	public String createOrderNo() {
		return SerialCreateUtil.generateByTime()+SerialCreateUtil.eachLength(4,getEndCode(DateUtil.getDateString()));
	}

    @Override
    public PageVo getActivityList(Long activityId, Integer start, Integer limit) {
        return new PageVo(start, limit, this.activityEnrollDao.countEnrol(activityId), this.activityEnrollDao.getEnrollList(activityId, start, limit));
    }

    private Long getEndCode(String date) {
		Example ex = new Example(ActivityEnroll.class);
		String dm =
				DateUtil.getDMTime(DateUtil.TIME_NUMBER,DateUtil.stringToDate(date,DateUtil.DATE));
		if(dm.length()>8){
			dm = dm.substring(0,8);
		}
		ex.createCriteria().andLike("enrollNo",'%'+dm+'%');
		Long count = activityEnrollDao.getCountByExample(ex);
		if(count>0){
			String code = activityEnrollDao.endNo(dm);
			return Long.parseLong(code.substring(code.length()-4));
		}else{

			return 0L;
		}

	}

	private Integer getEnrollCount(Long activityId){
		ActivityEnrollQuery activityEnrollQuery = new ActivityEnrollQuery();
		activityEnrollQuery.setActivityId(activityId);
		Long count =activityEnrollDao.countActivityEnroll(activityEnrollQuery);
		return count==0?0:count.intValue();
	}

}
